<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//引入auth门面
use Auth;
//引入
// use Session;
use DB;
use Session;

class PublicController extends Controller
{
    //展示登录页面
    public function login(){
    	//展示登录页面
    	return view('admin.public.login');
    }

    //验证用户
    public function checkLogin(Request $request){


    	//开始自动验证
    	$this -> validate($request,[
    		//验证字段 => 规则1|规则2|规则3:参数|规则4...
    		//用户名，必填，最少2位，最长20位
    		'username'	=>	'required|min:2|max:20',
    		//密码，必填，最少6位，最长30位
    		'password'	=>	'required|min:6|max:30',
    		//验证码，必填，固定5位，必须合法验证码
    		'captcha'	=>	'required|size:5|captcha'
    	]);
    	//获取用户名和密码
    	$data = $request -> only('username','password');

        //将登录用户的id，传入session
        // $session_id = DB::table('admin') -> where('username','=',$data['username']) -> get('id');
        // Session::put('action_id',$session_id);

    	$data['status'] = '2';
    	//开始用户信息的认证
    	$result = Auth::guard('admin') -> attempt($data,$request -> get('online'));
    	
    	//跳转302的响应
    	if($result){
            // $iddd = DB::table('admin') -> where($data['username'],'=','username') -> get('id');
            // $iddd = DB::table('admin') -> where('username','=',$baocun_data['username']) -> get('id');
            //日志记录==S
            $actions = [
                // 'id'          => 'null',   
                'action_name' => '用户登录',
                'action_time' => date('Y-m-d H:i:s'),
                'action_biao' => 'ib_admin',
                // 'action_project' => $iddd, 
                'action_project' => '',
                'admin_id'    => Session::get('login_admin_59ba36addc2b2f9401580f014c7f58ea4e30989d')
            ];
            DB::table('action') -> insert($actions);
            //日志记录==E


    		//合法用户，跳转到后台首页
    		return redirect('/admin/index/index');
    	}else{
    		//非法用户，跳转登录页面
    		return redirect('/admin/public/login') -> withErrors([
    			'loginError' =>	'用户名或密码错误！'
    		]);
    	}
    }

    //退出方法
    public function logout(){
    	
    	//跳转到登录页面
            
        //日志记录==S
            $actions = [
                // 'id'          => 'null',   
                'action_name' => '用户登出',
                'action_time' => date('Y-m-d H:i:s'),
                'action_biao' => 'ib_admin',
                // 'action_project' => $iddd, 
                'action_project' => '',
                'admin_id'    => Session::get('login_admin_59ba36addc2b2f9401580f014c7f58ea4e30989d')
            ];
            DB::table('action') -> insert($actions);
            //日志记录==E

            //清空session信息
        Auth::guard('admin') -> logout();

    	return redirect('/admin/public/login');
    }
}
